#include "Data.h"
Data *Data::instance = nullptr;
Data *Data::getInstance()
{
    if (instance == nullptr)
    {
        instance = new Data();
    }
    return instance;
}

void Data::load()
{
    try
    {
        loadImage();
        loadData("datas_list", "datas_list.json");
        loadData("config","config.json");
        CollisionManagement::iteratorCount = getData("config")->get("collisioniteratorcount")->getInt();
        ym::Json *datas = getData("datas_list");
        if (datas->isArray())
        {
            ym::Json::Array array = datas->getArray();
            for (auto it = array.begin(); it != array.end(); ++it)
            {
                if ((*it)->isArray())
                {
                    ym::Json::Array key_value = (*it)->getArray();
                    loadData(key_value.at(0)->getString().c_str(), key_value.at(1)->getString().c_str());
                }
            }
        }
        SDL_Log("load done");
    }
    catch (std::runtime_error &err)
    {
        throw err;
    }
}

void Data::loadImage()
{
    try
    {
        loadData("images_list", "images_list.json");
        ym::Json *images = getData("images_list");

        if (images->isArray())
        {
            const ym::Json::Array array = images->getArray();
            for (auto it = array.begin(); it != array.end(); ++it)
            {
                if ((*it)->isArray())
                {
                    const ym::Json::Array key_value = (*it)->getArray();
                    std::string imagedirectstr = key_value.at(2)->getString();
                    ImageDirect imgdirect;
                    if (!imagedirectstr.compare("down"))
                    {
                        imgdirect = ImageDirect::DOWN;
                    }
                    else if (!imagedirectstr.compare("left"))
                    {
                        imgdirect = ImageDirect::LEFT;
                    }
                    else if (!imagedirectstr.compare("right"))
                    {
                        imgdirect = ImageDirect::RIGHT;
                    }
                    else
                    {
                        imgdirect = ImageDirect::UP;
                    }
                    Resource::getInstance()->loadImage(key_value.at(0)->getString().c_str(), key_value.at(1)->getString().c_str(),imgdirect);
                }
            }
        }
    }
    catch (std::runtime_error &err)
    {
        throw err;
    }
}

void Data::loadAnimation()
{
}

void Data::quit()
{
    datas_map.clear();
    for (auto it = datas_map.begin(); it != datas_map.end();)
    {
        delete it->second;
        it = datas_map.erase(it);
    }
    delete instance;
    instance = nullptr;
}

void Data::setPath(const char *path)
{
    sprintf_s(datapath, sizeof(datapath), "%s\\data", path);
}

void Data::loadData(const char *name, const char *path)
{
    ym::Parser parser;
    char buff[BUFSIZ];
    sprintf_s(buff, sizeof(buff), "%s\\%s", datapath, path);
    try
    {
        ym::Json *data = parser.loadFromFile(buff);
        datas_map.emplace(name, data);
        SDL_Log("loadData %s done", buff);
    }
    catch (std::runtime_error &err)
    {
        throw std::runtime_error(std::string("没有找到:") + buff);
    };
}

ym::Json *Data::getData(const char *name)
{
    auto it = datas_map.find(name);
    return it != datas_map.end() ? (it->second) : nullptr;
}
